package com.quanshu.shop.goods.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.goods.entity.CartBaseEntity;
import com.quanshu.shop.goods.entity.GoodsPropertyBaseEntity;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/7/31
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class MyCartAdapter extends BaseQuickAdapter<CartBaseEntity, BaseViewHolder> {
    private Context mContext;

    public MyCartAdapter(@Nullable List<CartBaseEntity> data, Context context1) {
        super(R.layout.item_cart, data);
        mContext = context1;
    }

    @Override
    protected void convert(BaseViewHolder helper, CartBaseEntity item) {
        helper.setText(R.id.cart_txt_goods_title, item.getGoodsListEntity().getName());
        Glide.with(mContext).load(Config.URL_HOST_SHOP + item.getGoodsListEntity().getImg()).into((ImageView) helper.getView(R.id.cart_iv_goods));
        if (!TextUtils.isEmpty(item.getGoodsListEntity().getPayment_options())) {
            String s = Integer.parseInt(item.getGoodsListEntity().getPayment_options()) == 0 ? "现金支付" : "牛积分兑换";
            helper.setText(R.id.cart_txt_goods_payment, s);
        } else {
            helper.setText(R.id.cart_txt_goods_payment, "未知错误");
        }
        helper.setText(R.id.cart_txt_goods_money, item.getGoodsListEntity().getSum());
        helper.setText(R.id.cart_txt_goods_money1, item.getGoodsListEntity().getCount_score());

        if (!TextUtils.isEmpty(item.getGoodsListEntity().getPayment_options())) {
            if (Integer.parseInt(item.getGoodsListEntity().getPayment_options()) == 0) {
                helper.setVisible(R.id.cart_txt_goods_money, true);
                helper.setVisible(R.id.cart_txt_goods_money1, false);
            } else {
                helper.setVisible(R.id.cart_txt_goods_money1, true);
                helper.setVisible(R.id.cart_txt_goods_money, false);
            }
        } else {
            helper.setVisible(R.id.cart_txt_goods_money, true);
            helper.setVisible(R.id.cart_txt_goods_money1, false);
        }

        helper.setText(R.id.cart_txt_goods_number2, item.getGoodsListEntity().getCount());
        if (!TextUtils.isEmpty(item.getGoodsListEntity().getSpec_array())) {
            List<GoodsPropertyBaseEntity> goodsPropertyBaseEntityList = JSON.parseArray(item.getGoodsListEntity().getSpec_array(), GoodsPropertyBaseEntity.class);
            StringBuffer textR = new StringBuffer();
            if (goodsPropertyBaseEntityList != null && goodsPropertyBaseEntityList.size() > 0) {
                for (int a = 0; a < goodsPropertyBaseEntityList.size(); a++) {
                    textR.append(goodsPropertyBaseEntityList.get(a).getValue());
                    textR.append(",");
                }
                helper.setText(R.id.cart_txt_goods_type, textR.substring(0, textR.length() - 1).toString());
            }

        } else {
            helper.setText(R.id.cart_txt_goods_type, "默认规格");
        }
        if (item.isReduce()) {
            helper.getView(R.id.cart_txt_goods_number1).setEnabled(true);
        } else {
            helper.getView(R.id.cart_txt_goods_number1).setEnabled(false);
        }
        if (item.isPlus()) {
            helper.getView(R.id.cart_txt_goods_number3).setEnabled(true);
        } else {
            helper.getView(R.id.cart_txt_goods_number3).setEnabled(false);
        }
        ImageView imageView = helper.getView(R.id.cart_iv_select);
        if (item.isSelect()) {
            imageView.setImageResource(R.drawable.icon_selected_1_2_0);
        } else {
            imageView.setImageResource(R.drawable.icon_unselected_1_2_0);
        }
        if (Integer.parseInt(item.getGoodsListEntity().getIs_service())==1){
            //包含大礼包
            helper.setVisible(R.id.cart_txt_goods_payment1,true);
        }else {
            //不包含大礼包
            helper.setVisible(R.id.cart_txt_goods_payment1,false);
        }
        helper.addOnClickListener(R.id.cart_fl_select);
        helper.addOnClickListener(R.id.cart_txt_goods_number3);
        helper.addOnClickListener(R.id.cart_txt_goods_number1);
        helper.addOnClickListener(R.id.cart_txt_goods_number2);
    }
}
